﻿using System;
using System.Linq;
using Biz.EF.Platform;
using Hjt212Parser;
using log4net;


namespace TcpServer.DataProcessor
{
    class Platform2031ToMonthProcessor 
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Platform2031ToMonthProcessor));

      

        public static void Persist(DustContext context, string msg)
        {
            var mn = Parser.GetStringByKey("MN", msg);

            if (string.IsNullOrWhiteSpace(mn)) return;
            try
            {
                //有效字符串
                if (Parser.IsLengthRight(msg))
                {
                    //根据MN号获取基本信息
                    var equipment = BaseInfoBufferAndHelper.GetBaseWholeInfoByMn(mn);
                    //有效企业信息
                    if (equipment != null)
                    {
                        var datatime = Parser.GetDateTimeByKey("DataTime", msg);
                        //从字符串获取污染物代码s
                        var pollutantCodes = Parser.GetPollutantCodes(msg);
                        if (datatime.HasValue)
                        {
                            var main =
                                context.MonthDataRCMains.FirstOrDefault(t => t.MonitorTime.Year == datatime.Value.Year && t.MonitorTime.Month == datatime.Value.Month);
                            if (main == null)
                            {
                                main = new MonthDataRCMain()
                                {
                                    StatCodeID = equipment.StatCodeID,
                                    DevCodeID = equipment.DevCodeID,
                                    MonitorTime = datatime.Value
                                };
                                context.MonthDataRCMains.Add(main);
                            }
                            var mainSource =
                                context.V_DayData.Where(
                                    t =>
                                        t.MonitorTime.Year == datatime.Value.Year &&
                                        t.MonitorTime.Month == datatime.Value.Month &&
                                        t.StatCodeID == equipment.StatCodeID && t.DevCodeID == equipment.DevCodeID).ToList();

                            foreach (var code in pollutantCodes)
                            {
                                var factor = BaseInfoBufferAndHelper.GetFactorByCode(code);
                                if (factor == null) continue;

                                var detail =
                                    main.MonthDataRCDetails.FirstOrDefault(t => t.MonitorFactorID == factor.ID);
                                if (detail == null)
                                {
                                    detail = new MonthDataRCDetail()
                                    {
                                        MonitorFactorID = factor.ID
                                    };
                                    main.MonthDataRCDetails.Add(detail);
                                }
                                detail.Avg = mainSource.Where(t => t.MonitorFactorID == factor.ID).Average(t => t.Avg);
                                detail.Min = mainSource.Where(t => t.MonitorFactorID == factor.ID).Min(t => t.Min);
                                detail.Max = mainSource.Where(t => t.MonitorFactorID == factor.ID).Max(t => t.Max);
                            }
                            context.SaveChanges();
                        }
                    }
                    else
                    {
                        log.Warn( mn + " 没有对应基础信息！");
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error( ex);
                log.Error(msg);

            }
        }
     
    }
}
